from fastapi import APIRouter, HTTPException
from common.log import log_algo, log_method_io
from module.TextTransfer import TextTransfer

# 创建路由实例
router = APIRouter(
    prefix="/api/v1",
    tags=["文本处理"]
)

@router.post("/transfer")
@log_method_io
async def transfer_text(post: dict):
    """
    文本处理接口
    
    Args:
        post: 包含text字段的请求体
    
    Returns:
        dict: 包含处理结果的字典
    """
    try:
        # 从请求体中获取text字段(必选)
        text = post.get('text')
        if not text:
            raise HTTPException(
                status_code=400,
                detail="请求体中缺少text字段"
            )
        # 从请求体中获取keywords字段(可选)
        keywords = post.get('keywords')
        # 调用TextTransfer处理文本
        raw_data, transfer_data, similarity = TextTransfer().apply(text, keywords)

        # 返回处理结果
        return {
            "raw_text": raw_data,
            "transfer_text": transfer_data,
            "similarity": similarity
        }
        
    except Exception as e:
        log_algo.error(f"文本处理失败: {str(e)}")
        raise HTTPException(
            status_code=500,
            detail=f"文本处理失败: {str(e)}"
        )
